package com.good.controller;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

@Controller
@RequestMapping("/file")
public class Upload {

	@RequestMapping(value = "/upload1")
	public String addUser(@RequestParam("filename") CommonsMultipartFile file,
			HttpServletRequest request) throws IOException {
		FileOutputStream os = null;
		InputStream in = file.getInputStream();
		if (!file.isEmpty()) {
			try {
				String realPath = request.getSession().getServletContext()
						.getRealPath("/WEB-INF/upload");
				os = new FileOutputStream(realPath + new Date().getTime()
						+ file.getOriginalFilename());
				int b = 0;
				while ((b = in.read()) != -1) {
					os.write(b);
				}
				os.flush();

			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} finally {
				os.close();
				in.close();
			}
		}
		System.out.println("file1");
		return "success";
	}
}
